1.6 How many NAs?

na_df = purrr::map(dat_list, function(SAMPLE){
  out = tibble::tibble(N = nrow(SAMPLE$TRACKING),
                       N_NA = SAMPLE$TRACKING %>% 
                         dplyr::filter(!complete.cases(.)) %>% 
                         nrow(.)) %>% 
    dplyr::mutate(PROP_NA = N_NA / N )
  return(out)
}) %>% 
  dplyr::bind_rows(.id = "SAMPLE")



# Plot

na_plot = na_df %>% 
  ggplot() +
    geom_histogram(aes(N_NA), bins = 200)

ggplotly(na_plot)

1.6.1 Filter data list for those with more than 10% NAs

to_filter_out = na_df %>% 
  dplyr::filter(N_NA > 1800) %>% 
  dplyr::pull(SAMPLE)

dat_filt = dat_list[!(names(dat_list) %in% to_filter_out)]

# Get rid of raw data
dat_filt = dat_filt %>% 
  purrr::map(., function(SAMPLE) {
    purrr::keep(SAMPLE, names(SAMPLE) %in% c("META", "TRACKING"))
  })

1.6.2 Plot tracking data

With assistance from here: https://github.com/ropensci/plotly/issues/957

test = dat_filt$`20190611_1331_icab_icab_R_q1_novel_object`$TRACKING

test2 = list(A = test %>% 
               dplyr::select(COORD_X = X1,
                             COORD_Y = Y1) %>% 
               dplyr::mutate(FRAME = seq(1:nrow(.))),
             B = test %>% 
               dplyr::select(COORD_X = X2,
                             COORD_Y = Y2) %>% 
               dplyr::mutate(FRAME = seq(1:nrow(.)))) %>% 
  dplyr::bind_rows(.id = "FISH") %>% 
  # select every 30th frame
  dplyr::filter(FRAME %in% seq(from = 1, to = nrow(test), by = 30))

accumulate_by <- function(dat, var) {
  var <- lazyeval::f_eval(var, dat)
  lvls <- plotly:::getLevels(var)
  dats <- lapply(seq_along(lvls), function(x) {
    cbind(dat[var %in% lvls[seq(1, x)], ], frame = lvls[[x]])
  })
  dplyr::bind_rows(dats)
}

df = test2 %>%
  accumulate_by(~FRAME)

df %>% 
  plot_ly(x = ~COORD_X, y = ~COORD_Y) %>% 
  add_paths(color = ~FISH,
            line = list(simplify = F),
            frame = ~frame) %>% 
  layout(yaxis = list(autorange = "reversed")) %>% 
  layout(xaxis = list(scaleanchor = "y", scaleratio = 1))
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels